package com.llu.sha;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class SHATest {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String data = "Hello, World!";

        // 生成SHA-256哈希值
        String hash = generateSHA256Hash(data);
        System.out.println("SHA-256 Hash (Base64): " + hash);
    }

    public static String generateSHA256Hash(String data) throws NoSuchAlgorithmException {
        // 获取SHA-256消息摘要实例
        MessageDigest digest = MessageDigest.getInstance("SHA-256");

        // 计算哈希值
        byte[] hashBytes = digest.digest(data.getBytes(StandardCharsets.UTF_8));

        // 将哈希值转换为Base64编码字符串
        return Base64.getEncoder().encodeToString(hashBytes);
    }
}

